<?php
/**
CREATE TABLE `oc_address` (
`address_id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`fullname` varchar(64) NOT NULL,
`telephone` varchar(32) NOT NULL,
`company` varchar(60) NOT NULL,
`address_1` varchar(128) NOT NULL,
`address_2` varchar(128) NOT NULL,
`city` varchar(128) NOT NULL,
`postcode` varchar(10) NOT NULL,
`country_id` int(11) NOT NULL DEFAULT '0',
`zone_id` int(11) NOT NULL DEFAULT '0',
`custom_field` text NOT NULL,
`city_id` int(11) NOT NULL DEFAULT '0',
`county_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`address_id`),
KEY `customer_id` (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
 */
namespace App\Models\Order;

use App\Models\Location\City;
use App\Models\Location\Country;
use App\Models\Location\Zone;
use App\Services\Utils\Guard;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Address
 *
 * @property bool is_default - 是否为当前客户默认地址
 * @property int customer_id
 * @property string city - oc 表存设计问题，表有 city 字段但未使用，获取关联请使用 ->getRelation('city')
 *
 * @property string country_name
 * @property string zone_name
 * @property string city_name
 * @property string county_name
 *
 * @property-read Country country
 * @property-read Zone zone
 * @property-read City county
 * @property string country_id
 * @property string zone_id
 *
 * @package App\Models\Customers
 *
 * @mixin Builder
 */
class Address extends Model
{
    protected $table = 'address';

    protected $guarded = [];

    protected $primaryKey = 'address_id';

    public $timestamps = false;

    protected $casts = [
        'customer_id' => 'int',
    ];

    const POSTCODE_VARCHAR_LEN = 10;
    const ADDRESS_VARCHAR_LEN = 128;
    const FULL_NAME_VARCHAR_LEN = 64;
    const TEL_PHONE_VARCHAR_LEN = 32;
    const COMPANY_VARCHAR_LEN = 60;

    /**
     * @used-by $country
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function country()
    {
        return $this->belongsTo(Country::class, 'country_id', 'country_id');
    }

    /**
     * @used-by $zone
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function zone()
    {
        return $this->belongsTo(Zone::class, 'zone_id', 'zone_id');
    }

    /**
     * @used-by $city
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function city()
    {
        return $this->belongsTo(City::class, 'city_id', 'city_id');
    }

    /**
     * @used-by $county
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function county()
    {
        return $this->belongsTo(City::class, 'county_id', 'city_id');
    }

    /**
     * 数据库默认 not null
     *
     * @return array
     */
    static public function defaultFields()
    {
        return [
            'city'         => '',
            'address_2'    => '',
            'postcode'     => '',
            'company'      => '',
            'custom_field' => '',
        ];
    }

    /**
     * @param $address_id
     * @return Address|Model|null|object
     */
    public static function getAddress($address_id)
    {
        return (new self)->where('address_id', $address_id)
            ->where('customer_id', Guard::customer()->customer_id)
            ->first();
    }

    /**
     * @param $order_id
     * @return array
     */
    public static function getCountryAndZoneId($order_id)
    {
        $order = Order::getOrder($order_id);

        $shipping_data = [
            'country_id' => $order->shipping_country_id ?? null,
            'zone_id'    => $order->shipping_zone_id ?? null,
        ];

        return $shipping_data;
    }
}
